package org.veridiandynamics.server.domain;


import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * Test method.
 * 
 * Illustrates the set up and use of Envers.
 * <p>
 * This example is different from the others in that we really need to save multiple revisions to the entity in
 * order to get a good look at Envers in action.
 *
 * @author Steve Ebersole
 */
public class EnversIllustrationTest{
	
	/**
	 * Main method.
	 * 
	 * @param arg
	 */
	public static void main(String arg[])
	{
		SessionFactory sessionFactory;
	    sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save( new Event( "Our very first event!", new Date() ) );
		session.save( new Event( "A follow up event1", new Date() ) );
		session.getTransaction().commit();
		session.close();
		
		// now lets pull events from the database and list them
		session = sessionFactory.openSession();
        session.beginTransaction();
        List result = session.createQuery( "from Event" ).list();
		for ( Event event : (List<Event>) result ) {
			System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
		}
        session.getTransaction().commit();
        session.close();

        
        //update
		session = sessionFactory.openSession();
        session.beginTransaction();
        Event myevent=(Event) session.load( Event.class, 8L);
        myevent.setTitle("CHANGE1");
                session.getTransaction().commit();
        System.out.println("hiRetrieved Event ID:"+ myevent.getId()+"Event Title"+ myevent.getTitle());
        
        
        session.close();

        //update2
		session = sessionFactory.openSession();
        session.beginTransaction();
        myevent.setTitle("CHANGED5656");
        session.save(myevent);
                session.getTransaction().commit();
        System.out.println("Retrieved Event ID:"+ myevent.getId()+"Event Title"+ myevent.getTitle());
        
        
        session.close();        
        //delete
		session = sessionFactory.openSession();
        session.beginTransaction();
        //myevent=(Event) session.load( Event.class, 1l);
        session.delete(myevent);
        session.getTransaction().commit();
        session.close();

        
	}
	
}